package com.itheima.service.impl;

import com.itheima.dao.UserInfoDao;
import com.itheima.domain.UserInfo;
import com.itheima.factory.MapperFactory;
import com.itheima.service.UserInfoService;
import com.itheima.utils.MD5Util;
import com.itheima.utils.TransactionUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.Map;

public class UserInfoServiceImpl implements UserInfoService {

    /*
    * 登陆
    * */
    @Override
    public UserInfo login(String username, String password) {
        //密码加密
        String pwd = MD5Util.md5(password);

        //获取SqlSession对象
            SqlSession sqlSession = MapperFactory.getSqlSession();
        //获取持久层对象
        UserInfoDao mapper = MapperFactory.getMapper(sqlSession,UserInfoDao.class);
        //调用持久层方法
        UserInfo userInfo = mapper.login(username,pwd);

        //关闭资源
        TransactionUtil.close(sqlSession);

        return userInfo;
    }
    /*
    * 修改密码
    * */
    @Override
    public void updateById(String id ,String password) {
        String pwd = MD5Util.md5(password);


            //获取SqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();
            //获取持久层对象
            UserInfoDao mapper = MapperFactory.getMapper(sqlSession,UserInfoDao.class);
            //调用持久层方法
            mapper.updateById(id,pwd);
            //提交事物
            TransactionUtil.commit(sqlSession);
            //关闭资源
            TransactionUtil.close(sqlSession);
        }

    @Override
    public UserInfo ShowAll(String id) {
        //获取SqlSession对象
        SqlSession sqlSession = MapperFactory.getSqlSession();

        //获取持久层对象
        UserInfoDao mapper = MapperFactory.getMapper(sqlSession,UserInfoDao.class);

        //调用持久层方法
        UserInfo userInfo1 = mapper.ShowAll(id);
        return userInfo1;
    }

}
